package com.luichi.ygj.view.activity;

import android.os.Bundle;
import android.support.design.widget.CoordinatorLayout;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.luichi.data.base.IRequestId;
import com.luichi.domain.entity.LoginResult;
import com.luichi.ygj.R;
import com.luichi.ygj.presenter.ILoginView;
import com.luichi.ygj.presenter.LoginPresenter;
import com.ruhaly.androidsdk.baseapi.AndroidUtils;
import com.ruhaly.androidsdk.baseapi.BaseActivity;
import com.ruhaly.androidsdk.widget.ClearEditText;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import nucleus.factory.RequiresPresenter;

@RequiresPresenter(LoginPresenter.class)
public class LoginActivity extends BaseActivity<LoginPresenter> implements ILoginView {

    @Bind(R.id.etName)
    ClearEditText etName;
    @Bind(R.id.etPwd)
    ClearEditText etPwd;
    @Bind(R.id.tvFindPwd)
    TextView tvFindPwd;
    @Bind(R.id.btnLogin)
    Button btnLogin;
    @Bind(R.id.btnRegister)
    Button btnRegister;

    @Override
    public void initData() {

    }

    @Override
    public void initLayout(Bundle paramBundle) {
        setContentView(R.layout.activity_login);
    }

    @Override
    public void initView() {

    }

    @OnClick(R.id.btnLogin)
    public void btnLoginClick(View view) {
        if (!checkAllInput()) {
            return;
        }
        showLoading(IRequestId.REQUEST_LOGIN);
        getPresenter().requestLogin(etName.getText().toString(), etPwd.getText().toString());
    }

    private boolean checkAllInput() {
        String username = etName.getText().toString();
        if (TextUtils.isEmpty(username)) {
            showSnake("手机号不能为空");
            return false;
        }
        if (!AndroidUtils.isPhone(username)) {
            showSnake("手机号或密码错误");
            return false;
        }
        String password = etPwd.getText().toString();
        if (TextUtils.isEmpty(password)) {
            showSnake("密码不能为空");
            return false;
        }
        return true;
    }

    @Override
    public void notifyData(LoginResult result) {

    }

    @Override
    public void stopRequest(int requestId) {
        getPresenter().cancelRequest(requestId);
    }
}
